<?php
	include("includes/secure.php");
	include("includes/config.php");
	include_once("includes/class/email.class.php");
	include_once("includes/class/client.class.php");
	include_once("includes/class/user.class.php");
	require_once("includes/fckeditor/fckeditor_php5.php");
	require_once("includes/class/fileManagement.class.php");
		
	$email = new Email();
	$client = new Client();
	$user = new User();
	$file = new fileManagement();
	
	$msg_info = $msg_alert = $msg_error = false;
	$_SESSION['msg_cool'] = '';
	$show_msg = 0;
	
	$email_to = '';	
	if ( isset ( $_GET['cli_code'] ) && (int) $_GET['cli_code'] > 0 ){		
		$dataClient = $client->getClientData($_GET['cli_code']);
		$email_to = $dataClient->cli_email;
	}elseif ( isset ( $_GET['use_code'] ) && (int) $_GET['use_code'] > 0 ){		
		$dataUser = $user->getUserData($_GET['use_code']);
		$email_to = $dataUser->use_email;
	}
	
	if( isset ($_POST['add']) && $_POST['add'] == 1){
		$data->use_code = $_SESSION['use_code'];
		$data->email_subject = mysql_real_escape_string(trim(strip_tags($_POST['email_subject'])));
		$data->email_content = mysql_real_escape_string(trim(strip_tags($_POST['email_content'])));
		$data->email_to = mysql_real_escape_string(trim(strip_tags($_POST['email_to'])));
		$data->email_from = $_SESSION['use_email'];
		
		$dataEmail->email_body = $data->email_content;
		$dataEmail->email_address[0] = $data->email_to;
		$dataEmail->email_from = $data->email_from;
		$dataEmail->email_subject = $data->email_subject;
		
		$array_files;
		$k = 0;
		if ( isset ($_FILES['file']) ){
			for ($i = 0; $i < sizeof ($_FILES['file']['tmp_name']); $i++){
				if ( is_uploaded_file ($_FILES['file']['tmp_name'][$i]) ){
					$array_files[$i]->attach_name = '';
					$array_files[$i]->attach_rename = '';
					
					$document = $file->subirArchivo($_FILES['file']['tmp_name'][$i],$_FILES['file']['name'][$i],'file_upload/email_attachment/');
					
					$dataEmail->email_attachment[$k] = ('file_upload/email_attachment/'.$document);
					$dataEmail->email_attachment_name[$k] = ($_FILES['file']['name'][$i]);
					
					if($document!= '.'){
						$array_files[$k]->attach_name = $_FILES['file']['name'][$i];
						$array_files[$k]->attach_rename = $document;
						$k++;
					}					
				} 					
			}				
		}
		
		if ( sendApplicationEmail($dataEmail) ){
			$msg_info = true;
			$_SESSION['msg_cool'] .= 'Email sent successfully.<br />';
			
			if ( $email_code = $email->addEmail($data) ){
				$count_array_files = count ($array_files);
				if ( $count_array_files > 0) {
					for($i = 0; $i < $count_array_files; $i++){
						$array_files[$i]->email_code = $email_code;
						
						if(!$email->addAttachment($array_files[$i])){
							$msg_alert = true;
							$_SESSION["msg_cool"] .= $array_files[$i]->attach_name." hast not been saved.<br />";
						}
					}
				}
				echo ("<script> window.location.href='listEmail.php?show_msg=3' </script>");
			}
			else{
				$msg_error = true;
				$_SESSION["msg_cool"] .= 'The shipping record of email has not been saved..<br />';
			}
		}
		else{
			$msg_error = true;
			$_SESSION["msg_cool"] .= 'It could not be sending the email.<br />';
		}
	}
	
	if ( $msg_error )
		$show_msg = 1;
	elseif ( $msg_alert )
		$show_msg = 2;
	elseif ( $msg_info )
		$show_msg = 3;
	
	$fckeditor = new FCKeditor('email_content');
	$fckeditor->ToolbarSet = "Basic";
	$fckeditor->Height = "300";
	$fckeditor->Width = "720";
?>